﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class MechanicalRemarkRepo
    {
        public const string m_msg_head = "MechanicalRemark";

        public IEnumerable<MechanicalRemarkModels> GetMechanicalRemark()
        {
            List<MechanicalRemarkModels> MechanicalRemarks = new List<MechanicalRemarkModels>();
            string SQLCommand = @"select * from TBMechanicalRemark WHERE Status <> 'Delete'";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        MechanicalRemarkModels MechanicalRemark = new MechanicalRemarkModels();
                        MechanicalRemark.Id = (int)reader["Id"];
                        MechanicalRemark.TitleMechanicalType = (string)reader["TitleMechanicalType"];
                        MechanicalRemark.MechanicalRemarkTh = (string)reader["MechanicalRemarkTh"];
                        MechanicalRemark.MechanicalRemarkEng = (string)reader["MechanicalRemarkEng"];
                        MechanicalRemark.Status = (string)reader["Status"];
                        MechanicalRemark.CreateBy = (string)reader["CreateBy"];
                        MechanicalRemark.CreateOn = (DateTime)reader["CreateOn"];
                        MechanicalRemark.UpdateBy = (string)reader["UpdateBy"];
                        MechanicalRemark.UpdateOn = (DateTime)reader["UpdateOn"];

                        MechanicalRemarks.Add(MechanicalRemark);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return MechanicalRemarks.ToList();
        }

        public MechanicalRemarkModels GetMechanicalRemarkById(int id)
        {
            MechanicalRemarkModels MechanicalRemark = new MechanicalRemarkModels();
            string SQLCommand = @"select * from TBMechanicalRemark where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@Id", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        MechanicalRemark.Id = (int)reader["Id"];
                        MechanicalRemark.TitleMechanicalType = (string)reader["TitleMechanicalType"];
                        MechanicalRemark.MechanicalRemarkTh = (string)reader["MechanicalRemarkTh"];
                        MechanicalRemark.MechanicalRemarkEng = (string)reader["MechanicalRemarkEng"];
                        MechanicalRemark.Status = (string)reader["Status"];
                        MechanicalRemark.CreateBy = (string)reader["CreateBy"];
                        MechanicalRemark.CreateOn = (DateTime)reader["CreateOn"];
                        MechanicalRemark.UpdateBy = (string)reader["UpdateBy"];
                        MechanicalRemark.UpdateOn = (DateTime)reader["UpdateOn"];
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return MechanicalRemark;
        }

        public bool Create(MechanicalRemarkModels MechanicalRemark)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBMechanicalRemark 
                                    values (@TitleMechanicalType,@MechanicalRemarkTh,@MechanicalRemarkEng,@Status,@UserId,
                                    GETDATE(),@UserId,GETDATE())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleMechanicalType", MechanicalRemark.TitleMechanicalType);
                    db.AddParameter("@MechanicalRemarkTh", MechanicalRemark.MechanicalRemarkTh);
                    db.AddParameter("@MechanicalRemarkEng", MechanicalRemark.MechanicalRemarkEng);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID", MechanicalRemark.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(MechanicalRemarkModels MechanicalRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBMechanicalRemark 
                                    Set TitleMechanicalType=@TitleMechanicalType,
                                        MechanicalRemarkTh=@MechanicalRemarkTh, 
                                        MechanicalRemarkEng=@MechanicalRemarkEng,
                                        UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", MechanicalRemark.Id);
                    db.AddParameter("@TitleMechanicalType", MechanicalRemark.TitleMechanicalType);
                    db.AddParameter("@MechanicalRemarkTh", MechanicalRemark.MechanicalRemarkTh);
                    db.AddParameter("@MechanicalRemarkEng", MechanicalRemark.MechanicalRemarkEng);
                    db.AddParameter("@UserID", MechanicalRemark.UpdateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(MechanicalRemarkModels MechanicalRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBMechanicalRemark 
                                    Set Status=@Status,UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", MechanicalRemark.Id);
                    db.AddParameter("@Status", "Delete");
                    db.AddParameter("@UserID", MechanicalRemark.UpdateBy );

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}